import shelvesApi from "@/api/store/shelves";
import dictApi from "@/api/system/dict";
export default {
  name: "shelves",
  created() {
     this.init();
  },
  computed:{
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: false,
      listQuery: {
        current: 1,
        size: 10,
        criteria: {
            goodsType: null,
            storeHouseName: "",
            goodsName: "",
            supplierName: "",
        },
      },
      formVisible:false,
      baseUrl:process.env.VUE_APP_BASE_API,
      goodsTypeList:[],
      form:{
        id:'',
        salePrice:0,
        status:'',
        price:0,
      }
    };
  },
  methods: {
    init() {
      this.fetchData();
      this.getTypeDict();
    },
    fetchData() {
      this.listLoading = true;
      shelvesApi
        .getList(this.listQuery)
        .then((response) => {
          this.list = response.data.records;
          this.listLoading = false;
          this.total = response.data.total;
        })
        .catch(() => {});
    },
    formatDate(originVal) {
        const dt = new Date(originVal);
        const y = dt.getFullYear();
        const m = (dt.getMonth() + 1 + "").padStart(2, "0");
        const d = (dt.getDate() + "").padStart(2, "0");
        const hh = (dt.getHours() + "").padStart(2, "0");
        const mm = (dt.getMinutes() + "").padStart(2, "0");
        const ss = (dt.getSeconds() + "").padStart(2, "0");
        return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
    },
    generNum() {
      return Math.floor(Math.random() * 20);
    },
    getTypeDict() {
      // 从后台获取字典列表
      let dictName = "商品类型";
      dictApi.getDicts(dictName).then((response) => {
        this.goodsTypeList = response.data[0].children ?? [];
      });
    },
    getTypeValueDict(num) {
      const arr = this.goodsTypeList;
      if (arr) {
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].num == num) {
            return arr[i].name;
          }
        }
      }
    },
    generNum() {
        return Math.floor(Math.random() * 20);
      },
    search() {
      this.listQuery.current = 1;
      this.fetchData();
    },
    reset() {
      this.listQuery.criteria.ingoodsTypeBoundStatus = "";
      this.listQuery.criteria.storeHouseName = "";
      this.listQuery.criteria.goodsName = "";
      this.listQuery.criteria.supplierName = "";
      this.listQuery.current = 1;
      this.fetchData();
    },
    handleFilter() {
      this.listQuery.current = 1;
      this.fetchData();
    },
    handleClose() {},
    fetchNext() {
      this.listQuery.current = this.listQuery.current + 1;
      this.fetchData();
    },
    fetchPrev() {
      this.listQuery.current = this.listQuery.current - 1;
      this.fetchData();
    },
    fetchPage(page) {
      this.listQuery.current = page;
      this.fetchData();
    },
    changeSize(limit) {
      this.listQuery.size = limit;
      this.fetchData();
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow;
    },
    putGoods(row){
        this.form.id = row.id
        this.form.salePrice = row.salePrice
        this.form.price = row.price
        this.formVisible = true
    },
    submitForm(){
        this.$refs["form"].validate((valid) => {
            if (valid) {
                if(this.form.price>this.form.salePrice){
                    this.$confirm('销售价低于进价，是否继续上架?', '提示', {
                              confirmButtonText: '确定',
                              cancelButtonText: '取消',
                              type: 'warning'
                            }).then(() => {
                                shelvesApi.putGoods(this.form).then((response) => {
                                    this.$message({
                                        message: "提交成功",
                                        type: "success"
                                    });
                                    this.fetchData();
                                    this.formVisible = false;
                                });
                            }).catch(() => {
                            })

                }else{
                shelvesApi.putGoods(this.form).then((response) => {
                    this.$message({
                        message: "提交成功",
                        type: "success"
                    });
                    this.fetchData();
                    this.formVisible = false;
                });
            }
            } else {
                return false;
            }
        });
    },
    downGoods(id){
        if(!id){
            return;
        }
        shelvesApi.downGoods(id).then((response) => {
            this.$message({
                message: "下架成功",
                type: "success"
            });
            this.fetchData();
        });
    }
  },
};
